/*******************************************************************/
/*   RTX-51 uVision2 Debug-Functions                               */
/*                                                                 */
/*******************************************************************/
/* Copyright 1991 .. 2001 METTLER  &  FUCHS  AG,  CH-8953 Dietikon */
/*******************************************************************/

func void tasks ()  {
   unsigned char i;
   unsigned char tid;
   unsigned char temp;
   unsigned char temp2;
   unsigned char state_1;
   unsigned char state_2;
                                                                
   printf ("+----+-------+------+---------+-------------------+---------+-------+--------+\n");       
   printf ("| ID | Start | Prio | State   | Blocked for Event | Mbx/Sem | Timer | Signal |\n");
   printf ("+----+-------+------+---------+-------------------+---------+-------+--------+");       
            
   for (i = 0; i <= 18; i++)  {
      tid = _RBYTE (&?RTX_TID_TABLE+i);
      if (tid != 0xef) {
         /* Task-ID */
         temp = _RBYTE (&?RTX_INTERNAL_NUM_TABLE+i);
         printf ("\n|%3u | ", (unsigned int) temp);

         /* Start-Address / Prio */
         temp2 = 0;
         while (temp2 < 255) {
            if (_RBYTE (c:RTX_TASKCONST+temp2*7+4) == temp) break;
            temp2++;
         }
         printf ("%04XH | ", _RWORD (c:RTX_TASKCONST+temp2*7));
         printf ("  %1u  | ", _RBYTE (c:RTX_TASKCONST+temp2*7+3));

         /* Task-State */
         state_1 = _RBYTE (&?RTX_STATE_TABLE+i*4);
         state_2 = _RBYTE (&?RTX_STATE_TABLE+i*4+1) & 0x80;
         if (tid == _RBYTE(&?RTX_RUNNING_TID)) printf ("RUNNING | ");
         else if ((state_1 & 0x0f) | state_2)  printf ("BLOCKED | ");
         else                                  printf ("READY   | ");

         /* Blocked for Event / Mailbox / Timer */
         temp = _RBYTE (&?RTX_DELAY_LIST_CLK+i);
         temp = temp - _RBYTE (&?RTX_SYSCLK_COUNT);
         temp2 = (state_1 & 0x70) / 16;
         switch ((state_1 & 0x0f) | (state_2 / 8)) {
            case 0:  printf ("                  |         |       | ");
                     break;
            case 1:  printf ("MSG               |    %1u    |       | ", temp2);
                     break;
            case 2:  printf ("INT               |         |       | ");
                     break;
            case 3:  printf ("MSG & INT         |    %1u    |       | ", temp2);
                     break;
            case 4:  printf ("SIG               |         |       | ");
                     break;
            case 5:  printf ("MSG & SIG         |    %1u    |       | ", temp2);
                     break;
            case 6:  printf ("INT & SIG         |         |       | ");
                     break;
            case 7:  printf ("MSG & INT & SIG   |    %1u    |       | ", temp2);
                     break;
            case 8:  printf ("TMO               |         |  %3u  | ", (unsigned int) temp);
                     break;
            case 9:  printf ("MSG & TMO         |    %1u    |  %3u  | ", temp2 , (unsigned int) temp);
                     break;
            case 10: printf ("INT & TMO         |         |  %3u  | ", temp);
                     break;
            case 11: printf ("MSG & INT & TMO   |    %1u    |  %3u  | ", temp2 , (unsigned int) temp);
                     break;
            case 12: printf ("SIG & TMO         |         |  %3u  | ", (unsigned int) temp);
                     break;
            case 13: printf ("MSG & SIG & TMO   |    %1u    |  %3u  | ", temp2 , (unsigned int) temp);
                     break;
            case 14: printf ("INT & SIG & TMO   |         |  %3u  | ", (unsigned int) temp);
                     break;
            case 15: printf ("MSG&INT&SIG&TMO   |    %1u    |  %3u  | ", temp2 , (unsigned int) temp);
                     break;
            case 16: printf ("WRITE-MAILBOX     |    %1u    |       | ", temp2);
                     break;
            case 17: printf ("TKN               |    %2u   |       | ", temp2+8);
                     break;
            case 19: printf ("TKN & INT         |    %2u   |       | ", temp2+8);
                     break;
            case 21: printf ("TKN & SIG         |    %2u   |       | ", temp2+8);
                     break;
            case 23: printf ("TKN & INT & SIG   |    %2u   |       | ", temp2+8);
                     break;
            case 24: printf ("WRITE-MBX & TMO   |    %1u    |  %3u  | ", temp2 , (unsigned int) temp);
                     break;
            case 25: printf ("TKN & TMO         |    %2u   |  %3u  | ", temp2+8 , (unsigned int) temp);
                     break;
            case 27: printf ("TKN & INT & TMO   |    %2u   |  %3u  | ", temp2+8 , (unsigned int) temp);
                     break;
            case 29: printf ("TKN & SIG & TMO   |    %2u   |  %3u  | ", temp2+8 , (unsigned int) temp);
                     break;
            case 31: printf ("TKN&INT&SIG&TMO   |    %2u   |  %3u  | ", temp2+8 , (unsigned int) temp);
                     break;
            default: printf ("ILLEGAL           |         |       | ");
         }

         /* Signal */
         if (state_1 & 0x80) printf ("   1   |");
         else                printf ("   0   |");
      }
   }
   printf ("\n+----+-------+------+---------+-------------------+---------+-------+--------+\n");       
}


func void mailboxes ()  {
   unsigned char i;
   unsigned char k;
   unsigned char temp;
   unsigned int  msg;
   unsigned int  m_ofs;

   printf ("+-----+-----+------+-------+-------------------------------------------------+\n");
   printf ("| Mbx | Msg | Read | Write | Messages                                        |\n");
   printf ("+-----+-----+------+-------+-------------------------------------------------+");

   for (i = 0; i <= 7; i++)  {
      printf ("\n|  %1u  | ", (unsigned int) i);
      temp = _RBYTE (&?RTX_MBX_CONTROL_BASE+i*8);
      if (temp == 0)       printf (" 0  |   0  | ");
      else if (temp < 128) printf ("%2u  |   0  | ", (unsigned int) temp);
      else                 printf (" 0  |  %2u  | ", (unsigned int) ~temp+1);
      temp = _RBYTE (&?RTX_MBX_CONTROL_BASE+i*8+1);
      printf (" %2u   | ", (unsigned int) temp);
      temp = _RBYTE (&?RTX_MBX_CONTROL_BASE+i*8);
      if (temp < 128) {
         m_ofs = _RBYTE (&?RTX_MBX_CONTROL_BASE+i*8+2);
         for (k = 0; k <=7; k++) {
            if (k < temp) {
               msg = _RBYTE (&?RTX_MBX_PAGE+m_ofs)*256;
               m_ofs = (m_ofs+1) & 0xef;
               msg = msg + _RBYTE (&?RTX_MBX_PAGE+m_ofs);
               m_ofs = (m_ofs+1) & 0xef;
               if (k+1 == temp ) printf ("%04XH ", (unsigned int) msg);
               else              printf ("%04XH/", (unsigned int) msg);
            }
            else printf ("      ");
         }
         printf ("|");
      }
      else printf ("                                                |");
   }
   printf ("\n+-----+-----+------+-------+-------------------------------------------------+\n");
}



func void semaphores ()  {
   unsigned char i;
   unsigned char k;
   unsigned char temp1;
   unsigned char temp2;
   unsigned char cnt;

   printf ("+-----+-----+------+\n");
   printf ("| Sem | Tkn | Wait |\n");
   printf ("+-----+-----+------+");

   for (i = 0; i <= 7; i++)  {
      printf ("\n|  %2u | ", (unsigned int) i+8);
      temp1 = _RBYTE (&?RTX_SEM_TOKENS+i);
      if (temp1 == 0) printf (" 0  | ");
      else            printf (" 1  | ");
      temp1 = _RBYTE (&?RTX_MBX_CONTROL_BASE+i*8+6);
      temp2 = _RBYTE (&?RTX_MBX_CONTROL_BASE+i*8+7);
      cnt = 0;
         while (temp1 != temp2) {
            if ((temp1 & 0x0F) == 0x0F) temp1 &= 0xF0;
            else temp1++;
            cnt++;
         }
      printf (" %2u  |", cnt);
   }
   printf ("\n+-----+-----+------+\n");
}



func unsigned char read_x_char (unsigned int x_adr) {
   /* read char from xdata */
	return _RBYTE(x:0H+x_adr);
}

func void mempools ()  {
   unsigned char i;
   unsigned char j;
   unsigned char k;
   unsigned char taskid;
   unsigned char numblocks;
   unsigned char output;
   unsigned int  blksize;
   unsigned int  poolptr;

   output = 0;
   for (i = 0; i <= 15; i++)  {
      blksize = _RWORD (&?RTX_POOL_CONTR+i*6);
      numblocks = _RBYTE (&?RTX_POOL_CONTR+i*6+5);
      if (blksize != 0) {
         output = 1;
         printf ("+-------------------------------------------------------------------+\n");
         printf ("| Memory Pool No. = %2u     Blocksize = %5u (%04XH) Bytes          |\n", (unsigned int) i, blksize, blksize);
         printf ("+----+-----------+----+-----------+----+-----------+----+-----------+\n");
         printf ("|Task|Block/Addr |Task|Block/Addr |Task|Block/Addr |Task|Block/Addr |\n");
         printf ("+----+-----------+----+-----------+----+-----------+----+-----------+");
         poolptr   = _RWORD (&?RTX_POOL_CONTR+i*6+2);
         for (j = 0; j < numblocks; j += 4) {
            printf ("\n");
            for (k = 0; k < 4; k++) {
               if ((k+j) >= numblocks) {
                  printf ("|    |           ");
               } else {
                  taskid = read_x_char (poolptr+1);
                  if (taskid == 0xef) {
                     printf ("|free| %3u       ", (unsigned int)k+j );
                  } else {
                     taskid &= 0x1f;
                     taskid = _RBYTE (&?RTX_INTERNAL_NUM_TABLE + (unsigned int)taskid);
                     printf ("| %2u | %3u %04XH ", (unsigned int)taskid, (unsigned int)k+j, poolptr+2);
                     poolptr += blksize + 2;
                  }
               }
            }
            printf ("|");
         }
         printf ("\n+----+-----------+----+-----------+----+-----------+----+-----------+\n");
      }
   }
   if (output == 0) {
      printf ("+-------------------------+\n");
      printf ("+ No Memory Pools Defined +\n");
      printf ("+-------------------------+\n");
   }
}

define button "Task State", "tasks ()"
define button "Mailboxes",  "mailboxes ()"
define button "Semaphores", "semaphores ()"
define button "Mem.Pools",  "mempools ()"
